package com.zy.redisai;

import redis.clients.jedis.Jedis;
import java.util.*;
public class RedisRecommender {
    private Jedis jedis;
    public RedisRecommender() {
        // 连接Redis数据库
        jedis = new Jedis("192.168.133.105", 6379);
    }
    public void train(Map<String, List<String>> userHistory) {
        // 将用户历史行为数据存储到Redis中
        for (String user : userHistory.keySet()) {
            List<String> items = userHistory.get(user);
            for (String item : items) {
                jedis.zadd(user, 1, item);
            }
        }
    }
    public List<String> recommend(String user, int count) {
        // 计算用户之间的相似度，使用基于用户的协同过滤算法
        List<String> items = new ArrayList<>();
        List<String> users = new ArrayList<>();
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            if (!key.equals(user)) {
                users.add(key);
            }
        }
        for (String otherUser : users) {
            double similarity = similarity(user, otherUser);
            if (similarity > 0) {
                Set<String> diff = jedis.zdiff(otherUser, user);
                for (String item : diff) {
                    double score = jedis.zscore(otherUser, item) * similarity;
                    jedis.zadd("recommend:"+user, score, item);
                }
            }
        }
        // 生成推荐列表
        Set<String> topItems = jedis.zrevrange("recommend:"+user, 0, count - 1);
        for (String item : topItems) {
            items.add(item);
        }
        return items;
    }
    private double similarity(String user1, String user2) {
        Set<String> set2 = jedis.zrange(user2, 0, -1);
        Set<String> set1 = jedis.zrange(user1, 0, -1);
        Set<String> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);//从set1从删除set2中不存在的元素
        if (intersection.size() == 0) {
            return 0;
        }
        int union = set1.size() + set2.size() - intersection.size();//并集
        return (double) intersection.size() / union;
    }
    public static void main(String[] args) {
        // 测试
        RedisRecommender recommender = new RedisRecommender();
        Map<String, List<String>> userHistory = new HashMap<>();
        userHistory.put("user1", Arrays.asList("item1", "item2", "item3","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item13","item15","item16","item17","item18","item20"));
        userHistory.put("user2", Arrays.asList("item22", "item2", "item3","item4","item5","item6","item23","item33","item9","item22","item11","item30","item31","item32","item13","item15","item16","item17","item18","item20","item77"));
        userHistory.put("user3", Arrays.asList("item1", "item2", "item3","item4","item5","item6","item7","item30","item31","item32","item33","item12","item13","item14","item13","item15","item16","item17","item18","item20","item77"));
        userHistory.put("user4", Arrays.asList("item30", "item31", "item32","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item13","item15","item16","item17","item18","item20","item77"));



        recommender.train(userHistory);
        List<String> items = recommender.recommend("user1", 3);
        System.out.println(items);
    }
}